//////////////////////////////////////////////////////////////////////////////////////////////
//	File			:	"BattleMap.cpp"
//
//	Author			:	Mike Davis, Matt Stover, James Thornton, Keith Webster
//	
//	Project			:	TacWars
//
//	Last Modified	:	11/6/2013
//
//	Purpose			:	
//
//////////////////////////////////////////////////////////////////////////////////////////////
#pragma once
#include <iostream>
using namespace std;
#include "BattleMap.h"
#include "../StateMachine/GameplayState.h"
#include "../../SGD Wrappers/CSGD_TextureManager.h"
#include "../GameObjects/Cursor.h"

BattleMap::BattleMap()
{
	width = height = 0;
}
BattleMap::~BattleMap()
{
}
void BattleMap::CreateBattleMap(vector<TileM>&tile,int w,int h)
{
	width = w;
	height = h;
	for(unsigned int i = 0; i < tile.size(); i++)
	{
		Map.addVertex(&tile[i]);
		Map[i].element->OccupyingUnit = nullptr;
	}
	for(int y = 0; y < h; y++)
	{
		for(int x = 0; x < w; x++)
		{
			if(y == 0)
			{
				if(x != (w - 1))
				{
					Map[FindTile(x,y)].addEdge(FindTile(x+1,y));
					Map[FindTile(x,y)].addEdge(FindTile(x+1,y+1));
				}
				if(x != 0)
				{
					Map[FindTile(x,y)].addEdge(FindTile(x-1,y));
					Map[FindTile(x,y)].addEdge(FindTile(x-1,y + 1));
				}
				Map[FindTile(x,y)].addEdge(FindTile(x,y+1));
			}
			if(y == (h-1))
			{
				if(x != (w - 1))
				{
					Map[FindTile(x,y)].addEdge(FindTile(x+1,y));
					Map[FindTile(x,y)].addEdge(FindTile(x+1,y-1));
				}
				if(x != 0)
				{
					Map[FindTile(x,y)].addEdge(FindTile(x-1,y));
					Map[FindTile(x,y)].addEdge(FindTile(x-1,y - 1));
				}
				Map[FindTile(x,y)].addEdge(FindTile(x,y-1));
			}
			else
			{
				if(x != (w - 1))
				{
					Map[FindTile(x,y)].addEdge(FindTile(x+1,y));
					Map[FindTile(x,y)].addEdge(FindTile(x+1,y-1));
					Map[FindTile(x,y)].addEdge(FindTile(x+1,y+1));
				}
				if(x != 0)
				{
					Map[FindTile(x,y)].addEdge(FindTile(x-1,y));
					Map[FindTile(x,y)].addEdge(FindTile(x-1,y - 1));
					Map[FindTile(x,y)].addEdge(FindTile(x-1,y + 1));
				}
				Map[FindTile(x,y)].addEdge(FindTile(x,y-1));
				Map[FindTile(x,y)].addEdge(FindTile(x,y+1));
			}
		}
	}
	width = w;
	height = h;

}
void BattleMap::Render(int w, int h)
{
	CSGD_TextureManager* TM = CSGD_TextureManager::GetInstance();
	int cursorPosx = GameplayState::GetInstance()->GetCursor()->GetPosX() / 64;
	int cursorPosy = GameplayState::GetInstance()->GetCursor()->GetPosY() / 64;
	for(int y = max(0, cursorPosy - 5); y < min(cursorPosy + 5,height); y++)
	{
		for(int x = max(0, cursorPosx - 7); x < min(cursorPosx + 7,width); x++)
		{
			if(Map[FindTile(x,y)].element->position.x*Map[FindTile(x,y)].element->srcRect.right + 32- w > -64 && Map[FindTile(x,y)].element->position.x*Map[FindTile(x,y)].element->srcRect.right + 32- w < 800 && Map[FindTile(x,y)].element->position.y*Map[FindTile(x,y)].element->srcRect.bottom +32 - h > -64 && Map[FindTile(x,y)].element->position.y*Map[FindTile(x,y)].element->srcRect.bottom +32 - h < 600)
			{
			TM->Draw(Map[FindTile(x,y)].element->ImageId,Map[FindTile(x,y)].element->position.x*Map[FindTile(x,y)].element->srcRect.right + 32- w,Map[FindTile(x,y)].element->position.y*Map[FindTile(x,y)].element->srcRect.bottom +32 - h,1.0f,1.0f,&Map[FindTile(x,y)].element->srcRect);
			if(Map[FindTile(x,y)].element->OccupyingUnit != nullptr)
			TM->Draw(Map[FindTile(x,y)].element->OccupyingUnit->GetImageID(),Map[FindTile(x,y)].element->position.x*Map[FindTile(x,y)].element->srcRect.right + 32- w,Map[FindTile(x,y)].element->position.y*Map[FindTile(x,y)].element->srcRect.bottom +32 - h,1.0f,1.0f);
			}
		}
	}

}
int BattleMap::FindTile(int x, int y) const
{
	return (y * width + x);
}